package com.idp.common.model;

import com.idp.common.enums.MessageEnum;
import io.swagger.annotations.ApiModelProperty;

/**
 * JsonModel
 * @author king
 */
public class ResultModel<T> {

	private static final String SUCCESS_CODE = "0";
	private static final String ERROR_CODE = "10001";

	@ApiModelProperty("是否成功")
	private boolean success;
	@ApiModelProperty("错误编码")
	private String code;
	@ApiModelProperty("提示信息")
	private String message;
	@ApiModelProperty("结果集")
	private T data;

	public ResultModel() {
		super();
		this.success = true;
		this.code = "0";
		this.message = "";
	}

	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}

	public static ResultModel success() {

		ResultModel result = new ResultModel();
		result.setSuccess(Boolean.TRUE);
		result.setCode(SUCCESS_CODE);
		result.setMessage(MessageEnum.OPERATE_SUCCESS.message);

		return result;
	}

	public static <T> ResultModel<T> success(T data) {
		ResultModel result = new ResultModel();
		result.setSuccess(Boolean.TRUE);
		result.setCode(SUCCESS_CODE);
		result.setMessage(MessageEnum.OPERATE_SUCCESS.message);
		result.setData(data);

		return result;
	}

	public static <T> ResultModel<T> success(String message, T data) {
		ResultModel result = new ResultModel();
		result.setSuccess(Boolean.TRUE);
		result.setCode(SUCCESS_CODE);
		result.setMessage(message);
		result.setData(data);

		return result;
	}

	public static ResultModel fail() {

		ResultModel result = new ResultModel();
		result.setSuccess(Boolean.FALSE);
		result.setCode(ERROR_CODE);
		result.setMessage(MessageEnum.OPERATE_FAIL.message);

		return result;
	}

	public static ResultModel fail(String message) {

		ResultModel result = new ResultModel();
		result.setSuccess(Boolean.FALSE);
		result.setCode(ERROR_CODE);
		result.setMessage(message);

		return result;
	}

}
